//package com.example.task;
//
//import com.baomidou.dynamic.datasource.annotation.DSTransactional;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.example.entity.hz.IBody;
//import com.example.entity.hz.IHead;
//import com.example.entity.mysql.Resource;
//import com.example.entity.vo.pc.MoctyPlus;
//import com.example.entity.yifei.*;
//import com.example.enums.InspectStatusEnum;
//import com.example.enums.MachineStatusEnum;
//import com.example.enums.MessageTypeEnum;
//import com.example.enums.ZnxStatusEnum;
//import com.example.mapper.InspectiontimeMapper;
//import com.example.mapper.MoctyMapper;
//import com.example.mapper.QmsmgMapper;
//import com.example.service.*;
//import com.example.service.pc.ResourceService;
//import com.example.untils.BeanUtils;
//import com.example.untils.StringUtils;
//import com.example.untils.TimeUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import java.math.BigDecimal;
//import java.util.*;
//import java.util.concurrent.Executors;
//import java.util.concurrent.ScheduledExecutorService;
//import java.util.concurrent.TimeUnit;
//
//@Component
//@Order(Integer.MAX_VALUE)
//public class InspectionTask {
//
//    private final Random random = new Random();
//    private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
//
////    public InspectionTask() {
////        executeInspectionTask();
////    }
//    @Autowired
//    private ZnxService znxService;
//    @Autowired
//    private IHeadService iHeadService;
//    @Autowired
//    private InspectiontimeMapper inspectiontimeMapper;
//    @Autowired
//    private IBodyService iBodyService;
//
//    @Autowired
//    private ResourceService resourceService;
//
//    @Autowired
//    private MoctyService moctyService;
//
//    @Autowired
//    private MoctyMapper moctyMapper;
//
//    @Autowired
//    private MoctaService moctaService;
//
//    @Autowired
//    private QmsmgMapper qmsmgMapper;
//
//    @Autowired
//    private MoclogService moclogService;
//
//    @PostConstruct
//    @DSTransactional
//    public void executeInspectionTask() {
//        System.out.println("run executeInspectionTask");
//        List<Resource> resourceList = resourceService.list(new LambdaQueryWrapper<Resource>()
//                .and(i -> i.eq(Resource::getResourceType, 1).or().eq(Resource::getResourceType, 2)).eq(Resource::getStatus, MachineStatusEnum.JiaGong.getValue()));
//        List<String> uuidOrderList = new ArrayList<>();
//        List<Mocty> moctyOnDoList = new ArrayList<>();
//        for(Resource t : resourceList) {
//            Mocty mocty = moctyMapper.selectOne(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, t.getMoctyudf05()));
//            if(mocty == null) continue;
//            List<Mocty> moctyList = new ArrayList<>();
//            if(StringUtils.isNotEmpty(mocty.getUdf02())) {
//                moctyList = moctyMapper.selectList(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf02, mocty.getUdf02()));
//            }else{
//                moctyList.add(mocty);
//            }
//            moctyOnDoList.addAll(moctyList);
//            for(Mocty mocty1 : moctyList) {
//                uuidOrderList.add(mocty1.getUdf05() + "-" + t.getJinzhanUuid());
//            }
//        }
//        if(uuidOrderList.size() == 0) {
//            return;
//        }
//        List<MoctyPlus> moctyPlusList = qmsmgMapper.haveInspectionTime();
////        List<String> idid = qmsmgMapper.needXJList(uuidOrderList);  // 所有正在加工中的单子  && 已经做完了首检单的mocty
////        Set<String> idSet = new HashSet<>();
////        for(String t : idid) idSet.add(t);
////        List<Resource> resourceList1 = new ArrayList<>();
////        for(Resource item : resourceList) {
////            if(idSet.contains(item.getMoctyudf05().trim()+"-"+item.getJinzhanUuid())) {
////                resourceList1.add(item);
////            }
////        }
//        Long zhi = new Long(120);
//        Long x = new Long(60 * 2);
//        // TODO 这边的循环要改成循环mocty的单子   因为共模单的原因，之前resource的字段inspectionTime，无法使用
//        for(MoctyPlus moctyPlus : moctyPlusList) {
//            String time = TimeUtils.getNowTime();
//            if(StringUtils.isEmpty(moctyPlus.getInspectionTime())) continue;
//            Long difference = TimeUtils.getSecondsDifference(time, moctyPlus.getInspectionTime());  // difference 一定大于0
//            if(difference.compareTo(zhi) < 0) {   // 如果差值在里面则是不用做的
//                x = Math.min(x, zhi - difference);
//                continue;
//            }
//            // 否则需要做巡检单，&& 修改机器的巡检时间
//            QueryWrapper<Qmsmg> qmsmgQueryWrapper = new QueryWrapper<>();
//            qmsmgQueryWrapper.eq("MG002", moctyPlus.getProductId());// 品号
//            qmsmgQueryWrapper.eq("MG020", "过程检验"); //检验项目
//            List<Qmsmg> qmsmgs = qmsmgMapper.selectList(qmsmgQueryWrapper);
//            Map<String, List<Qmsmg>> map = new HashMap<>();
//            List<Qmsmg> qmsmgList = new ArrayList<>();
//            for(Qmsmg t : qmsmgs) {
//                qmsmgList.add(t);
//            }
//            map.put("现场", qmsmgList);
//            List<IHead> iHeadList = new ArrayList<>();
//            List<IBody> iBodyList = new ArrayList<>();
//            List<Moclog> moclogList = new ArrayList<>();
//            Long uid = qmsmgMapper.uid();  int idx = 0;
//            for (Map.Entry<String, List<Qmsmg>> entry : map.entrySet()) {
//                IHead iHead = new IHead();
//                iHead.setType("XJ");
//                iHead.setUid(uid + idx);  idx++;
//                iHead.setTa001(moctyPlus.getTy001());
//                iHead.setTa002(moctyPlus.getTy002());
//                iHead.setTa004(moctyPlus.getProcessId());
//                iHead.setTa024(moctyPlus.getProcessName());
//                iHead.setTa006(moctyPlus.getProductId());
//                iHead.setMb002(moctyPlus.getProductName());
//                iHead.setMb003(moctyPlus.getSpecification());
//                iHead.setTa015(new BigDecimal(moctyPlus.getTy004()));
//                iHead.setCreatTime(TimeUtils.getFullNowTime());
//                iHead.setOrderNum(qmsmgMapper.getOrderNumByJinZhanUuid(moctyPlus.getUdf05(), moctyPlus.getJinzhanUuid()));
//                iHead.setJinzhanUuid(moctyPlus.getJinzhanUuid());
//                //    iHead.setUdf88(new BigDecimal(3D));
//                iHead.setMoctyudf05(moctyPlus.getUdf05());
//                iHead.setStatus(InspectStatusEnum.DaiJianYan.getValue());
//                iHead.setUdf06(entry.getKey());
//                iHead.setMacNo(moctyPlus.getMacNo());
//                iHead.setMacName(moctyPlus.getMacName());
//                iHead.setWorkCenterID(moctyPlus.getWorkCenterId());
//                iHead.setWorkCenterName(moctyPlus.getWorkCenterName());
//                iHead.setMacPlaceID(moctyPlus.getMacPlaceID());
//                iHead.setMacPlaceName(moctyPlus.getMacPlaceName());
//                iHead.setApprovalCode("N");
//                iHead.setStatusCode("0");
//                iHeadList.add(iHead);
//
//                Moclog moclog = new Moclog();
//                moclog.setTa001(moctyPlus.getTy001());
//                moclog.setTa002(moctyPlus.getTy002());
//                moclog.setCreator("DS");
//                moclog.setCreatetime(TimeUtils.getFullNowTime());
//                moclog.setMoctyudf05(moctyPlus.getUdf05());
//                moclog.setRemark("生成巡检单: " + iHead.getType() + "-" + iHead.getUid());
//                moclog.setInspectiontype(iHead.getType());
//                moclog.setInspectionid(iHead.getUid());
//                moclogList.add(moclog);
//
//                List<Qmsmg> list = entry.getValue();
//                for(Qmsmg item : list) {
//                    IBody iBody = new IBody();
//                    iBody.setI_uid(iHead.getUid());
//                    BeanUtils.copyPropertiesIgnoreCase(item, iBody);
//                    iBodyList.add(iBody);
//                }
//
//                // 消息通知
//                znxService.saveZnxAndSendMessage(MessageTypeEnum.JIANYAN.getValue(),
//                        "有一张巡检单待处理, 单号：" + iHead.getUid(), ZnxStatusEnum.DAIBAN.getValue());
//
//                Inspectiontime inspectiontime = new Inspectiontime();
//                inspectiontime.setInspectiontime(time);
//                inspectiontimeMapper.update(inspectiontime, new LambdaQueryWrapper<Inspectiontime>().eq(Inspectiontime::getMoctyudf05, moctyPlus.getUdf05()));
//
//            }
//            iHeadService.saveBatch(iHeadList);
//            iBodyService.saveBatch(iBodyList);
//            moclogService.saveBatch(moclogList);
//
////            DynamicDataSourceContextHolder.push("mes");
//            // 修改resource中的巡检时间
////            Resource t = new Resource();
////            t.setInspectionTime(TimeUtils.getNowTime());  // 修改resource中的巡检时间
////            resourceService.update(t, new LambdaQueryWrapper<Resource>().eq(Resource::getResourceGroupIdIsF, resource.getResourceGroupIdIsF()));
//        }
//        executorService.schedule(this::executeInspectionTask, x, TimeUnit.SECONDS);
//    }
//
//}
